﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;
using System.Text;

namespace FCNS.KeepMoneyLibrary.Data
{
    public class BankField
    {
        public decimal AvailableCredit()
        {
            decimal de = 0;
            foreach (RegularEntryField rf in RegularEntryField.GetRegularEntryField.FindAll(gref =>
            { return gref.BankId == this.Id && gref.Finish != gref.columnTime; }))
            {
                if (rf.columnUseCredit)
                    de -= ((rf.columnTime - rf.Finish) * rf.MoneyPerTime);
            }

            return Credit + ((Balance > 0) ? 0 : Balance) + de;
        }

        public void SetBankBalance(decimal money)
        {
            Balance = Balance + money;
        }

        public static bool VerifyWhetherSufficientToPay(int bankId, decimal expenses)
        {
            Debug.Assert(fields.Count != 0);

            BankField bf = fields.Find(f => { return f.Id == bankId; });
            return (bf.Credit + bf.Balance - Math.Abs(expenses)) > 0;
        }


        static List<BankField> fields = new List<BankField>();
        public static List<BankField> GetBankField
        {
            get
            {
                if (fields.Count == 0)
                {
                    foreach (DataRow row in KMData.DataTableBanks.Rows)
                        fields.Add(new BankField(row));

                    Debug.Assert(fields.Count != 0, "BankField");
                }
                return fields;
            }
        }

        public void Remove()
        {
            //移除相应的账务记录
            foreach (EntryField f in EntryField.GetEntryField.FindAll(ef => { return ef.BankId == Id; }))
            {
                KMData.DataTableEntrys.Rows.Remove(f.Row);
                EntryField.GetEntryField.Remove(f);
            }

            KMData.DataTableBanks.Rows.Remove(dRow);
            fields.Remove(this);
        }

        bool add = false;
        public void AddNewRowToDataTable()
        {
            if (!add)
                return;

            KMData.DataTableBanks.Rows.Add(dRow);
            fields.Add(this);
            add = false;
        }

        DataRow dRow = null;
        public BankField(DataRow row)
        {
            Debug.Assert((row != null), "添加钱袋字段");

            dRow = row;
        }

        public BankField(string bankName)
        {
            dRow = KMData.DataTableBanks.NewRow();
            Name = bankName;
            add = true;
        }

        public string Url
        {
            get { return (string)dRow["Url"]; }
            set { dRow["Url"] = value; }
        }

        public decimal Balance
        {
            get { return (decimal)dRow["Balance"]; }
            set { dRow["Balance"] = value; }
        }

        public decimal Credit
        {
            get { return (decimal)dRow["Credit"]; }
            set { dRow["Credit"] = value; }
        }

        public int Id
        {
            get { return (int)dRow["Id"]; }
        }

        public string Name
        {
            get
            {
                return (string)dRow["Name"];
            }
            set { dRow["Name"] = value; }
        }

        public bool Default
        {
            get { return (bool)dRow["Default"]; }
            set { dRow["Default"] = value; }
        }

        /// <summary>
        /// 钱袋是否可见
        /// </summary>
        public bool Visible
        {
            get { return (bool)dRow["Visible"]; }
            set
            {
                dRow["Visible"] = value;
            }
        }

        /// <summary>
        /// 钱袋是否启用
        /// </summary>
        public bool Enable
        {
            get { return (bool)dRow["Enable"]; }
            set { dRow["Enable"] = value; }
        }
    }
}